//Data Cleaning//
use "C:\Users\user\Desktop\objective2\stataobjective2Data.dta" 
br
//Coding Hospital//
tab casesources
tab controlsources
gen Hospital= controlsources
order Hospital controlsources
tab controlsources
replace Hospital=casesources if controlsources=="" & casesources!=""
save,replace
encode Hospital, gen (Hospital1)
codebook Hospital1
tab Hospital1
describe Hospital1
save,replace
tab Hospital1
//Encoding case//
codebook case
gen Case1=1 if case=="Yes"
replace Case1=0 if case=="No"
tab Case1
label define Casc 1 "Yes" 0 "No"
label values Case1 Casc
codebook Case1
save,replace
tab Case1

//Encoding primary referenced cases//
tab sbd1

//Encoding secondsray/other cases in case group/
tab sbd2
tab sbd2 sbd1

//Encoding history of siblings with birth defects//
tab history_of_defects
tab history_of_defects Case1


//Generating Periconceptional Subcounty of residence//
gen CountyResid1=1 if sub_count_resid=="Thika"
replace CountyResid1=2 if sub_count_resid=="Gatundu"
replace CountyResid1=3 if sub_count_resid=="Kiambu"
replace CountyResid1=4 if sub_count_resid=="Ruiru"
replace CountyResid1=. if CountyResid1==4
replace CountyResid1=4 if sub_count_resid=="Ruiru"
replace CountyResid1=4 if sub_count_resid=="Ruiru"
replace CountyResid1=4 if sub_count_resid=="ruiru"
replace CountyResid1=5 if sub_count_resid!="ruiru" & sub_count_resid!="Ruiru" & sub_count_resid!="Kiambu" & sub_count_resid!="Gatundu" & sub_count_resid!="Thika"
tab CountyResid1
label define C 1 "Thika" 2 "Gatundu" 3 "Kiambu" 4 "Ruiru" 5 "Other Subcounties"
label values CountyResid1 C
codebook CountyResid1
save, replace
tab CountyResid1
describe CountyResid1
tab CountyResid1 Case1, col
logit Case1 ib5.CountyResid1 
logit, or
//LRT//
logit Case1 ib5.CountyResid1
est store a
logit Case1
est store b
lrtest a b


//MATERNAL OCCUPATION//

//Mothers Occupation//
tab mat_occup
gen Mat_Occupation1=1 if mat_occup=="farmer"
replace Mat_Occupation1=2 if mat_occup=="employed"
replace Mat_Occupation1=3 if mat_occup=="unemployed"
tab Mat_Occupation1
save,replacelogit Case1 1b2.patage
label define B 1 "farmer" 2 "employed" 3 "unemployed"
label values Mat_Occupation1 B
codebook Mat_Occupation1
save,replace
tab Mat_Occupation1 Case1, col
logit Case1 i.Mat_Occupation1
logit, or
//LRT//
logit Case1 i.Mat_Occupation1
est store a
logit Case1
est store b
lrtest a b



///GENERATING AND UNIVARIABLE ANALYSIS OF SOCIODEMOGRAPHIC FACTORS///
//MATERNAL AGE//
tab mat_age
sum mat_age
sum matage, detail
gen matage = mat_age
replace matage = 1 if (mat_age <=34)
replace matage = 2 if (mat_age >=35)
codebook matage
tab matage Case1, col
describe matage
tab matage
logit Case1 matage
logit, or
logit Case1 ib1.matage
logit, or

//MAT CASEAGE//
tab caseage
sum caseage
sum caseage, detail

//MAT CONTROLAGE//
tab controlage
sum controlage
sum controlage, detail



//PATERNAL AGE//
tab pat_age
sum pat_age
sum pat_age, detail
gen patage=1 if pat_age<=34
replace patage=2 if pat_age>=35 & pat_age!=.
tab patage
label define E 1 "<=34" 2 ">=35"
label values patage E
codebook patage 
label values patage E
codebook patage
save, replace
tab patage
tab patage Case1, col
logit Case1 1b2.patage
logit, or



//PAT CASEAGE//
tab casepatage
sum casepatage
sum casepatage, detail

//PAT CONTROLAGE//
tab controlpatage
sum controlpatage
sum controlpatage, detail



//MATERNAL EDUCATION LEVEL//
tab mat_educ
gen Mat_Education1=1 if mat_educ=="None"
replace Mat_Education1=2 if mat_educ=="Primary"
replace Mat_Education1=3 if mat_educ=="Secondary"
replace Mat_Education1=4 if mat_educ=="College Certificate"
replace Mat_Education1=5 if mat_educ=="College Diploma"
replace Mat_Education1=6 if mat_educ=="University Degree"
tab Mat_Education1
label define D 1 "None" 2 "Primary" 3 "Secondary" 4 "College Certificate" 5 "College Diploma" 6 "University Degree"
label values Mat_Education1 D
codebook Mat_Education1
save,replace
tab Mat_Education1
codebook Mat_Education1
gen MaternalEducation12=1 if Mat_Education1==1|Mat_Education1==2
replace MaternalEducation12=2 if Mat_Education1==3
replace MaternalEducation12=3 if Mat_Education1>3 & Mat_Education1!=.
tab MaternalEducation12
label define ma 1 "Primary and below" 2 "Secondary" 3 "Tertiary"
label values MaternalEducation12 ma
save,replace
codebook MaternalEducation12
tab MaternalEducation12 Case1,col
logit Case1 ib3.MaternalEducation12
logit, or
//LRT//
logit Case1 ib3.MaternalEducation12
est store a
logit Case1
est store b
lrtest a b


// GESTATIONAL AGE TO FIRST ANC VISIT//
tab gestwks_anc1
sum gestwks_anc1
gen gestwksanc1=1 if gestwks_anc1<=8
replace gestwksanc1=2 if gestwks_anc1>=9 & gestwks_anc1!=.
tab gestwksanc1
label define S 1 "<=8" 2 ">=9"
label values gestwksanc1 S
codebook gestwksanc1
label values gestwksanc1 S
codebook gestwksanc1
save,replace
tab gestwksanc1 
tab gestwksanc1 Case1, col
logit Case1 ib1.gestwksanc1
logit, or

//GESTATIONAL AGE OF CASES WEEKS TO 1ST ANC//
tab casegestwks
sum casegestwks

//GESTATIONAL AGE OF CONTROLS WEEKS TO 1ST ANC//
tab controlgestwks
sum controlgestwks

//PARITY//
tab gravidity
sum gravidity
gen Parity1=1 if gravidity==1
replace Parity1=2 if gravidity>=2 & gravidity!=.
tab Parity1
label define P 1 "=1" 2 ">1"
label values Parity1 p
codebook Parity1 Parity1
codebook Parity1
label values Parity1 P
codebook Parity1
save,replace
tab Parity1 
tab Parity1 Case1, col
logit Case1 Parity1
logit, or
logit Case1 ib1.Parity1
logit, or
tab Parity1

//PARITY OF CASES//
tab casegrav
sum casegrav

//PARITY OF CONTROLS//
tab controlgrav
sum controlgrav

//ANC BEGAN AFTER 8 WEEKS AFTER/POST CONCEPTION//
tab anc_lmp
tab anc_lmp
encode anc_lmp, gen(ANC_2monthsAfter)
codebook ANC_2monthsAfter
save,replace
tab anc_lmp Case1, col
tab ANC_2monthsAfter
logit Case1 i.ANC_2monthsAfter
logit, or


//GENERATING PRECONCEPTION FOLIC ACID INTAKE/ 3 MONTHS BEFORE LMP//
codebook fa_lmp
describe fa_lmp
tab fa_lmp
gen Folic_Acid= fa_lmp if fa_lmp!="Missing"
encode Folic_Acid,gen(Folic_Acid1)
tab Folic_Acid1
tab Folic_Acid1,m
tab Folic_Acid1 Case1, col
logit Case1 Folic_Acid1
logit, or
logit Case1 i.Folic_Acid1
logit, or

//GENERATING PESTICIDE-EXPOSURE//
tab farm_pest
encode farm_pest, gen (farm_pesticide)
codebook farm_pesticide
save,replace
tab farm_pesticide
tab farm_pesticide Case1, col
logit Case1 i.farm_pesticide
logit, or

///GENERATING AND ANALYSIS OF ENVIRONMENT-INHERITANCE INTERCTION FACTORS///
//GENERATING NATURE OF PREGNANCY//
tab preg_nature
encode preg_nature,gen(preg_nature1)
codebook preg_nature
codebook preg_nature1
save,replace
tab preg_nature1
tab preg_nature1 Case1, col
logit Case1 i.preg_nature1
logit, or
//GENERATING CHILD SEX//
tab child_sex
encode child_sex, gen(child_sex1)
codebook child_sex1
save,replace
tab child_sex1
tab child_sex1 Case1, col
logit Case1 i.child_sex1
logit, or

//GENERATING SIBLING WITH BIRTH DEFECTS//
tab child_defect
encode child_defect, gen (child_defect1)
codebook child_defect
codebook child_defect1
save,replace
tab child_defect1 Case1, col
logit Case1 i.child_defect1
logit, or
tab child_defect1
tab history_of_defects

///GENERATING AND UNIVARIABLE ANALYSIS OF ENVIROMENT-RELATED TERATOGENS//
//Generating Chronic illness//
tab chronic_illness
encode chronic_illness, gen(chronic_illness1)
codebook chronic_illness1
save,replace
tab chronic_illness1
codebook chronic_illness1
gen chronic_illness12=1 if chronic_illness1==6
replace chronic_illness12=2 if chronic_illness1==8
replace chronic_illness12=3 if chronic_illness1!=6 & chronic_illness1!=8
tab chronic_illness12
label define chronic 1 "Hypertension" 2 "No chronic illness" 3 "Other chronic illness"
label values chronic_illness12 chronic
save,replace
logit Case1 ib2.chronic_illness12 
logit, or
tab chronic_illness12 Case1,col
//LRT//
logit Case1 ib2.chronic_illness12
est store a
logit Case1
est store b
lrtest a b



///MULTIVARIABLE ANALAYSIS//
logi Case1 ib5.CountyResid1 i.child_defect1 ib2.matage i.Mat_Occupation1 i.preg_nature1 i.ANC_2monthsAfter ib1.gestwksanc1
logit, or

//MULTIVARIABLE ANALYSES; NATURE OF PREGNANCY DROPPED DUE TO COLLINEARITY//

logi Case1 ib5.CountyResid1 i.child_defect1 ib2.matage i.Mat_Occupation1 i.ANC_2monthsAfter ib1.gestwksanc1
logit, or












